#!/bin/bash -e

if [ $# -lt 1 ] ; then
  echo "USAGE: $0 <version> [registry [image]]"
  echo "  e.g.: $0 1.3.2 registry.my.org managed/php:1.3.2-test"
  exit 1
fi

VERSION=$1

if [ $# -ge 2 ]; then
  REGISTRY="$2/"
fi

docker image pull php:fpm-alpine

PHP_VERSION=`docker image inspect php:fpm-alpine | grep PHP_VERSION | awk -F '=' '{print $2}' | awk -F '"' '{print $1}' | uniq`
IMAGE="managed/php:${VERSION}-fpm-alpine"

if [ $# -ge 3 ]; then
  IMAGE=$3
fi

echo "PHP_VERSION: $PHP_VERSION"
echo "VERSION: $VERSION"
echo "REGISTRY: $2"
echo "IMAGE: $IMAGE"

docker image build --tag ${REGISTRY}${IMAGE}-${PHP_VERSION} --tag ${REGISTRY}${IMAGE} 7/fpm/alpine

if [ $# -ge 2 ]; then
  docker image push ${REGISTRY}${IMAGE}-${PHP_VERSION}
  docker image push ${REGISTRY}${IMAGE}
fi
